# -*- encoding: utf-8 -*-
'''
Created on Apr 1, 2012

@author: LONG HOANG GIANG
'''
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib.http import Http
import commonlib
from lxml import etree
from readability.readability import Document
import re
import simplejson as json

template = '''<html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="content-type" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="format-detection" content="telephone=no" />
<title>Anh Lại Cưa Em Nhé</title>
</head>
<body>
{0}
</body>
</html>
</html>
'''

FOLDER_PATH = '/anhlaicuaemnhe/'

def process2(start, end):
    
    if not os.path.exists(FOLDER_PATH): os.makedirs(os.path.dirname(FOLDER_PATH), 0777)
    
    for page in range(start, end+1):
        url = 'http://www.wattpad.com/apiv2/storytext?id=366797&page={0}'.format(page)
        html = Http.getHtml(url)
#        articleNode = commonlib.getArticleNode(html)
#        html = template.format(etree.tostring(articleNode, pretty_print=True, encoding='utf-8'))
#        html = re.sub("<p>Hãy nói yêu thôi, đừng nói yêu mãi mãi !</p>", "<h1>Hãy nói yêu thôi, đừng nói yêu mãi mãi !</h1>", html)
        html = json.loads(html)
        html = template.format(html)
        f = open('{0}{1}.html'.format(FOLDER_PATH, page), 'w')
        f.write(html)
        f.close()

def process(start, end):
    
    if not os.path.exists('/haynoiyeuthoi/'): os.makedirs('/haynoiyeuthoi', 0777)
    for page in range(start, end+1):
        url = 'http://www.wattpad.com/295394?p={0}'.format(page)
        html = Http.getHtml(url)
        articleNode = commonlib.getArticleNode(html)
        html = template.format(etree.tostring(articleNode, pretty_print=True, encoding='utf-8'))
        html = re.sub("<p>Hãy nói yêu thôi, đừng nói yêu mãi mãi !</p>", "<h1>Hãy nói yêu thôi, đừng nói yêu mãi mãi !</h1>", html)
        f = open('/haynoiyeuthoi/{0}.html'.format(page), 'w')
        f.write(html)
        f.close()

if __name__ == '__main__':
    
    process2(1, 108)
    print '>> Finished'
    os._exit(1)